package showtime.framework.net.rpc
{
	import flash.events.EventDispatcher;
	import flash.net.Responder;
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-1-8
	 *
	 */
	
	public class Operation extends EventDispatcher
	{
		private var service:BaseRpc = null;
		
		private var methodName:String = null;
		
		private var responder:Responder = null;
		
		public function Operation(service:BaseRpc, methodName:String)
		{
			this.service = service;
			
			this.methodName = methodName;
			
			if (service.asyncWaitingFor != null)
			{
				this.responder = new Responder(service.asyncWaitingFor, faultHandler);
			}
			else
			{
				this.responder = new Responder(resultHandler, faultHandler);
			}
			
		}
		
		public function getService():BaseRpc
		{
			return service;
		}
		
		public function getName():String
		{
			return methodName;
		}
		
		public function send(...arguments:Array):void
		{
			
			var params:Array = [service.destination + "." + methodName, responder];
			
			params = params.concat(arguments);
			
			service.getConnection().call.apply(service.getConnection(), params);
		}
		
		private function resultHandler(data:Object):void
		{
			trace("[RPC receive data] - " + data);
		}
		
		private function faultHandler(info:Object):void
		{
			trace("[RPC error] - " + info);
		}
	}
}